package com.huatai.exchange.bootup.listener;

import com.faneui.common.bean.BaseResp;
import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.Constants;
import com.faneui.common.utils.ListUtils;
import com.google.common.collect.Lists;
import com.huatai.exchange.dal.model.ExcCustDataFile;
import com.huatai.exchange.facade.Contents;
import com.huatai.exchange.facade.enums.CustDataFileTypeEnum;
import com.huatai.exchange.integration.support.service.ScheduleBatchService;
import com.huatai.exchange.service.ExcCustDataFileService;
import com.huatai.support.facade.bean.ScheduleBatchVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "checknightdatafilesendstatus")
@Api("任务")
public class NightDataFileSendStatusListener {

    private static final Logger logger = LoggerFactory.getLogger(NightDataFileSendStatusListener.class);

    @Autowired
    private ExcCustDataFileService custDataFileService;

    @Autowired
    private ScheduleBatchService scheduleBatchService;

    @Value("${server.port}")
    private String businSystemCode;

    @ApiModelProperty("计算当天清算任务是否结束")
    @RequestMapping(value = "", method = RequestMethod.GET)
    @ResponseBody
    public BaseResp run(@RequestParam(required = false) String occurDate) {
        BaseResp resp = new BaseResp();
        try {
            ExcCustDataFile param = new ExcCustDataFile();
            param.setType(CustDataFileTypeEnum.night_file.getCode());
            List<ExcCustDataFile> custDataFiles = custDataFileService.queryExcCustDataFileList(param);
            List<String> businSystemCodeAndIds = Lists.newArrayList();
            for (ExcCustDataFile e : custDataFiles) {
                String codeAndId = businSystemCode + Contents.busin_cust_data_id + e.getId();
                businSystemCodeAndIds.add(codeAndId);
            }
            ScheduleBatchVo batchParam = new ScheduleBatchVo();
            batchParam.setBusinSystemCodeAndIds(businSystemCodeAndIds);
            batchParam.setOccurDate(occurDate);
            List<ScheduleBatchVo> vos = scheduleBatchService.queryScheduleBatchVoList(batchParam);
            if(ListUtils.isEmpty(vos)) {
                throw new BusinessException("当日任务未开始") ;
            }
            boolean isFinish = true;
            int a = 0, b = 0;
            for (ScheduleBatchVo vo : vos) {
                a++;
                if (!Constants.PROCESS_STATUS_S.equalsIgnoreCase(vo.getDealStatus())) {
                    isFinish = false;
                    b++;
                }
            }
            if (!isFinish) {
                resp.setSuccess(false);
                String msg = "未完成，邮件生成任务总数[" + a + "]个，已完成[" + (a - b) + "]个，剩余[" + b + "]个";
                resp.setMsg(msg);
            } else {
                String msg = "已完成,邮件生成任务总数[" + a + "]个，已完成[" + (a - b) + "]个";
                resp.setMsg(msg);
            }
        } catch (Exception ex) {
            logger.error("", ex);
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }

//    @ApiModelProperty("计算当天清算任务是否结束")
//    @RequestMapping(value = "", method = RequestMethod.GET)
//    @ResponseBody
//    public BaseResp run12(@RequestParam(required = false) String occurDate) {
//        BaseResp resp = new BaseResp();
//        try {
//            BaseResp resp1 = this.run(occurDate) ;
//            if(resp1.isSuccess()) {
//                //TODO
//                // 发短信给相关的人
//            }
//        } catch (Exception ex) {
//            logger.error("", ex);
//            resp.setSuccess(false);
//            resp.setMsg(ex.getMessage());
//        }
//        return resp;
//    }

}
